package cn.stylefeng.guns.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.business.entity.WxInstall;
import cn.stylefeng.guns.modular.business.exception.WxInstallExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.WxInstallMapper;
import cn.stylefeng.guns.modular.business.pojo.WxInstallRequest;
import cn.stylefeng.guns.modular.business.service.WxInstallService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆管理业务实现层
 *
 * @author 施律名
 * @date 2022-03-10 17:48:59
 */
@Service
public class WxInstallServicelmpl extends ServiceImpl<WxInstallMapper, WxInstall> implements WxInstallService {

    @Override
    public void add(WxInstallRequest wxInstallRequest) {
        WxInstall wxInstall = new WxInstall();
        BeanUtil.copyProperties(wxInstallRequest, wxInstall);
        this.save(wxInstall);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(WxInstallRequest wxInstallRequest) {
        WxInstall wxInstall = this.queryWxInstall(wxInstallRequest);
        this.removeById(wxInstall.getWxInstallId());
    }

    @Override
    public void edit(WxInstallRequest wxInstallRequest) {
        WxInstall wxInstall = this.queryWxInstall(wxInstallRequest);
        BeanUtil.copyProperties(wxInstallRequest, wxInstall);
        this.updateById(wxInstall);
    }

    @Override
    public WxInstall detail(WxInstallRequest wxInstallRequest) {
        return this.queryWxInstall(wxInstallRequest);
    }

    @Override
    public PageResult<WxInstall> findPage(WxInstallRequest wxInstallRequest) {
        LambdaQueryWrapper<WxInstall> wrapper = createWrapper(wxInstallRequest);
        Page<WxInstall> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<WxInstall> findList(WxInstallRequest wxInstallRequest) {
        LambdaQueryWrapper<WxInstall> wrapper = this.createWrapper(wxInstallRequest);
        return this.list(wrapper);
    }

    /**
     * 获取车辆信息
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private WxInstall queryWxInstall(WxInstallRequest wxInstallRequest) {
        WxInstall wxInstall = this.getById(wxInstallRequest.getWxInstallId());
        if (ObjectUtil.isEmpty(wxInstall)) {
            throw new BusinessException(WxInstallExceptionEnum.WX_INSTALL_NOT_EXISTED);
        }
        return wxInstall;
    }

    /**
     * 创建查询wrapper
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private LambdaQueryWrapper<WxInstall> createWrapper(WxInstallRequest wxInstallRequest) {
        LambdaQueryWrapper<WxInstall> queryWrapper = new LambdaQueryWrapper<>();

        Long WxInstallId = wxInstallRequest.getWxInstallId();
        queryWrapper.like(ObjectUtil.isNotEmpty(WxInstallId), WxInstall::getWxInstallId, WxInstallId);

        // 根据时间倒序排列
        queryWrapper.orderByDesc(WxInstall::getCreateTime);

        return queryWrapper;
    }


}